#-*- encoding: utf-8 -*-
import win32com.client 
from Misc.Utils import *

from DataAccess.QueryBlp import *

from datetime import date,timedelta

from ReportTool.HldReport import *
from ReportTool.PnLReport import *
from ReportTool.NAVReport import *
from ReportTool.MktValReport import *
from ReportTool.TradeReport import *
from ReportTool.RealizedPnL import *
from ReportTool.OnshoreHldReport import *

from Config.ReportConfig import *
	
def Realized_PnL(ref_date, portf_list):

	#realized_pnl = query_realized_pnl_average_cost(ref_date, portf_list)
	realized_pnl = query_realized_pnl_first_in_first_out(ref_date, portf_list)
	
	tplt_file = TPLT_PATH + 'Realized PnL.xls'
	xApp = win32com.client.Dispatch("Excel.Application")
	xBook = xApp.Workbooks.Open(tplt_file)
	xSht = xBook.Worksheets('Report')
	
	row = 2
	for elem in realized_pnl:
	
		xSht.Range(xSht.Cells(row, 1), xSht.Cells(row, 10)).Value = elem
		row += 1
		
	rept_file = REPT_PATH + 'Realized PnL_' + ref_date.strftime("%Y%m%d") + '.xls'
	xBook.SaveAs(rept_file)
	xBook.Close()
	del xApp
	
	print 'info, realized pnl report completed at ', ref_date.isoformat()